package com.kdgc.power.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;


@Configuration
public class DataSourceConfig {

    @Bean(name = "mainDataSource")
    @Primary
    public DataSource mainDataSource(Environment env) {
        String url = env.getProperty("spring.datasource.primary.url");
        String username = env.getProperty("spring.datasource.primary.username");
        String password = env.getProperty("spring.datasource.primary.password");
        String driver = env.getProperty("spring.datasource.primary.driver-class-name");
        HikariDataSource ds = new HikariDataSource();
        ds.setJdbcUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setDriverClassName(driver);
        return ds;
    }
    @Bean(name = "secondaryDataSource")
    public DataSource secondaryDataSource(Environment env) {
        String url = env.getProperty("spring.datasource.secondary.url");
        String username = env.getProperty("spring.datasource.secondary.username");
        String password = env.getProperty("spring.datasource.secondary.password");
        String driver = env.getProperty("spring.datasource.secondary.driver-class-name");
        HikariDataSource ds = new HikariDataSource();
        ds.setJdbcUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setDriverClassName(driver);
        return ds;
    }
}